package web

import (
	"github.com/aphasia51/website/internal/domain"
	"github.com/aphasia51/website/internal/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

type WasteChemicalHandler struct {
	svc *service.WasteChemicalService
}

func NewWasteChemicalHandler(svc *service.WasteChemicalService) *WasteChemicalHandler {
	return &WasteChemicalHandler{
		svc: svc,
	}
}

func (a *WasteChemicalHandler) RegistryRoutes(server *gin.Engine) {
	ag := server.Group("/wasteChemical")
	ag.POST("/query", a.Query)
}

func (a *WasteChemicalHandler) Query(ctx *gin.Context) {
	type QueryReq struct {
		Year string `json:"year"`
	}
	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	WasteChemical, err := a.svc.Query(ctx, domain.WasteChemical{
		Year: req.Year,
	})

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, WasteChemical)
}
